package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Student;
import com.production.CoursePlatform.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 根据 sID 查询 Student
    @GetMapping("/findBySID/{sID}")
    public ResponseEntity<Student> findBySID(@PathVariable String sID) {
        Student student = studentService.findStudentBySID(sID);
        if (student != null) {
            return ResponseEntity.ok(student);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 添加 Student
    @PostMapping("/add")
    public ResponseEntity<String> addStudent(@RequestBody Student student) {
        int result = studentService.addStudent(student);
        if (result > 0) {
            return ResponseEntity.status(HttpStatus.CREATED).body("Student added successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add student");
        }
    }

    // 更新 Student
    @PutMapping("/update")
    public ResponseEntity<String> updateStudent(@RequestBody Student student) {
        int result = studentService.updateStudent(student);
        if (result > 0) {
            return ResponseEntity.ok("Student updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Failed to update student");
        }
    }

    // 根据 sID 删除 Student
    @DeleteMapping("/delete/{sID}")
    public ResponseEntity<String> deleteStudent(@PathVariable String sID) {
        int result = studentService.deleteStudent(sID);
        if (result > 0) {
            return ResponseEntity.ok("Student deleted successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Failed to delete student");
        }
    }
//
//    // 查询所有 Student
//    @GetMapping("/findAll")
//    public ResponseEntity<List<Student>> findAllStudents() {
//        List<Student> students = studentService.findAllStudents();
//        return ResponseEntity.ok(students);
//    }
}

